package com.yxq.mdt.app.widgets.countdown;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yxq.mdt.app.R;

/**
 * @author L
 * @time 2019-10-24
 * @describe 自定义倒计时View$
 */
public class CustomCountDownView extends LinearLayout {
    private BaseCountdown mCountdown;
    private CustomCountDownTimer mCustomCountDownTimer;
    private OnCountdownEndListener mOnCountdownEndListener;
    private OnCountdownIntervalListener mOnCountdownIntervalListener;
    private TextView mHourTextView;
    private TextView mMinuteTextView;
    private TextView mSecondTextView;
    private long mPreviousIntervalCallbackTime;
    private long mInterval;
    private long mRemainTime;
    private boolean isHideTimeBackground;

    public CustomCountDownView(Context context) {
        this(context, null);
    }

    public CustomCountDownView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomCountDownView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CountdownView);
        isHideTimeBackground = ta.getBoolean(R.styleable.CountdownView_isHideTimeBackground, true);

        mCountdown = isHideTimeBackground ? new BaseCountdown() : new BackgroundCountdown();
        mCountdown.initStyleAttr(context, ta);
        ta.recycle();

        mCountdown.initialize();
        init(context);
    }

    private void init(Context context) {
        View.inflate(context, R.layout.layout_count_down_layout, this);
        mHourTextView = findViewById(R.id.count_down_tv_hour);
        mMinuteTextView = findViewById(R.id.count_down_tv_minute);
        mSecondTextView = findViewById(R.id.count_down_tv_second);
    }

    public void start(long millisecond) {
        if (millisecond <= 0) return;

        mPreviousIntervalCallbackTime = 0;

        if (null != mCustomCountDownTimer) {
            mCustomCountDownTimer.stop();
            mCustomCountDownTimer = null;
        }

        long countDownInterval;
//        if (mCountdown.isShowMillisecond) {
//            countDownInterval = 10;
//            updateShow(millisecond);
//        } else {
        countDownInterval = 1000;
//        }

        mCustomCountDownTimer = new CustomCountDownTimer(millisecond, countDownInterval) {
            @Override
            public void onTick(long millisUntilFinished) {
                updateShow(millisUntilFinished);
            }

            @Override
            public void onFinish() {
                // countdown end
                allShowZero();
                // callback
                if (null != mOnCountdownEndListener) {
                    mOnCountdownEndListener.onEnd(CustomCountDownView.this);
                }
            }
        };
        mCustomCountDownTimer.start();
    }

    public void updateShow(long ms) {
        this.mRemainTime = ms;

        reSetTime(ms);

        // interval callback
        if (mInterval > 0 && null != mOnCountdownIntervalListener) {
            if (mPreviousIntervalCallbackTime == 0) {
                mPreviousIntervalCallbackTime = ms;
            } else if (ms + mInterval <= mPreviousIntervalCallbackTime) {
                mPreviousIntervalCallbackTime = ms;
                mOnCountdownIntervalListener.onInterval(this, mRemainTime);
            }
        }

        if (mCountdown.handlerAutoShowTime() || mCountdown.handlerDayLargeNinetyNine()) {
            reLayout();
        } else {
            //invalidate();
        }
    }

    private void reSetTime(long ms) {
        int day = 0;
        int hour;

        if (!mCountdown.isConvertDaysToHours) {
            day = (int) (ms / (1000 * 60 * 60 * 24));
            hour = (int) ((ms % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
        } else {
            hour = (int) (ms / (1000 * 60 * 60));
        }

        int minute = (int) ((ms % (1000 * 60 * 60)) / (1000 * 60));
        int second = (int) ((ms % (1000 * 60)) / 1000);
        int millisecond = (int) (ms % 1000);

        mCountdown.setTimes(day, hour, minute, second, millisecond);
        mHourTextView.setText(String.format("%02d", hour));
        mMinuteTextView.setText(String.format("%02d", minute));
        mSecondTextView.setText(String.format("%02d", second));
    }

    private void reLayout() {
        mCountdown.reLayout();

        //requestLayout();
    }

    public interface OnCountdownEndListener {
        void onEnd(CustomCountDownView cv);
    }

    public interface OnCountdownIntervalListener {
        void onInterval(CustomCountDownView cv, long remainTime);
    }

    /**
     * stop countdown
     */
    public void stop() {
        if (null != mCustomCountDownTimer) mCustomCountDownTimer.stop();
    }

    /**
     * set all time zero
     */
    public void allShowZero() {
        mCountdown.setTimes(0, 0, 0, 0, 0);
        invalidate();
    }

    public void setOnCountdownEndListener(OnCountdownEndListener mOnCountdownEndListener) {
        this.mOnCountdownEndListener = mOnCountdownEndListener;
    }
}
